package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.entity.InfDictItem;
import com.ctcemti.mdm.project.sys.service.InfDictItemService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName InfDictItemController
 * @Description 数据字典项表控制层
 * @Author bxf
 * @Date 8:32 2019/7/18
 * @Version V1.0
 **/

@RestController
@Api(value = "InfDictItemController", description = "数据字典项表控制类")
@RequestMapping("infdictitem")
public class InfDictItemController {
    @Autowired
    private InfDictItemService infDictItemService;

    @ApiOperation(value = "分页数据字典项")
    @PostMapping("selectListByPid")
    @ApiImplicitParam(name="infDictSort",value = "字典分类实体类")
    public Result<PageInfo> selectListByPid(HttpServletRequest request, HttpServletResponse response , InfDictItem infDictItem){

        PageInfo<Map<String,Object>> pageInfo = infDictItemService.selectListByPid(infDictItem);

        return new Result<PageInfo>(ResultEnum.SUCCESS,pageInfo);
    }

    @ApiOperation(value = "保存数据字典项")
    @PostMapping("insertInfDictItem")
    @ApiImplicitParam(name="InfDictItem",value = "数据字典项实体类")
    public Result<InfDictItem> insertInfDictItem(HttpServletRequest request, HttpServletResponse response , InfDictItem infDictItem){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        infDictItem.setCreator(loginId);
        infDictItemService.insertSelective(infDictItem);
        return new Result<InfDictItem>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "更新数据字典项")
    @PostMapping("updateInfDictItem")
    @ApiImplicitParam(name="InfDictItem",value = "数据字典项实体类")
    public Result<InfDictItem> updateInfDictItem(HttpServletRequest request, HttpServletResponse response, InfDictItem infDictItem){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        infDictItem.setModifiedBy(loginId);
        infDictItem.setModifyDate(new Date());
        infDictItemService.updateByIdSelective(infDictItem);
        return new Result<InfDictItem>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "获取所有的数据字典项子目")
    @PostMapping("selectAllInfDictItem")
    public Result<List<InfDictItem>> selectAllInfDictItem(HttpServletRequest request, HttpServletResponse response){
        List<InfDictItem> list = new ArrayList<InfDictItem>();
        list = infDictItemService.selectAllInfDictItem();
        return new Result<List<InfDictItem>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "根据字典code获取的数据字典项子目")
    @PostMapping("selectInfdictitemByInfdictsortCode")
    @ApiImplicitParam(name="code",value = "数据字典类编码")
    public Result<List<InfDictItem>> selectInfdictitemByInfdictsortCode(HttpServletRequest request, HttpServletResponse response,String code){
        List<InfDictItem> list = new ArrayList<InfDictItem>();
        list = infDictItemService.selectInfdictitemByInfdictsortCode(code);
        return new Result<List<InfDictItem>>(ResultEnum.SUCCESS,list);
    }


    @ApiOperation(value = "根据数据字典项项实体类获取list")
    @PostMapping("selectInfDictItemByEntity")
    @ApiImplicitParam(name="InfDictItem",value = "数据字典项项实体类")
    public Result<InfDictItem> selectInfDictItemByEntity(HttpServletRequest request, HttpServletResponse response,InfDictItem infDictItem){
        List<InfDictItem> list = new ArrayList<InfDictItem>();
        list = infDictItemService.selectInfDictItem(infDictItem);
        return new Result<InfDictItem>(ResultEnum.SUCCESS,list.size());
    }

    @ApiOperation(value = "根据ids删除数据字典项")
    @PostMapping("deleteInfDictItemByIds")
    @ApiImplicitParam(name="InfDictItem",value = "数据字典项实体类")
    public Result<InfDictItem> deleteInfDictItemByIds(HttpServletRequest request, HttpServletResponse response,String ids){
        List<InfDictItem> list = new ArrayList<InfDictItem>();
        int flag = infDictItemService.deleteByPrimaryKeys(ids);
        if(flag>0){
            return new Result<InfDictItem>(ResultEnum.SUCCESS,null);
        }else{
            return new Result<InfDictItem>(ResultEnum.ZTSJ_1014,null);
        }
    }
}
